
/***************************************************************************
 *   Copyright 2006-2009 by Christian Ihle                                 *
 *   kontakt@usikkert.net                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

package net.usikkert.kouchat.net;

import java.io.File;

import net.usikkert.kouchat.event.FileTransferListener;
import net.usikkert.kouchat.misc.User;
import net.usikkert.kouchat.misc.Settings;

/**
 * This is a mock implementation of the file transfer class, for use in unit tests.
 * It will simulate what a real file transfer behaves like, without touching any
 * physical file.
 *
 * @author Christian Ihle
 */
public class MockFileTransfer implements FileTransfer
{
	private final Direction direction;
	private final File file;
	private final User user;

	private FileTransferListener listener;
	private int percent;
	private long transferred;
	private boolean cancel;

	/**
	 * Constructor.
	 *
	 * @param direction If this mock should send or receive the file.
	 */
	public MockFileTransfer( final Direction direction )
	{
		this.direction = direction;
		user = new User( "TestUser", 1234 );
		user.setIpAddress( "192.168.1.1" );
		file = new File( "test/this_is_a_fake_test_file_with_a_very_very_long_file_name.txt" );
		Settings.getSettings().getMe().setIpAddress( "192.168.1.2" );
	}

	/**
	 * Aborts the file transfer simulation.
	 *
	 * {@inheritDoc}
	 */
	@Override
	public void cancel()
	{
		cancel = true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Direction getDirection()
	{
		return direction;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public File getFile()
	{
		return file;
	}

	/**
	 * Returns ~500KB.
	 *
	 * @return 500000.
	 */
	@Override
	public long getFileSize()
	{
		return 500000;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User getUser()
	{
		return user;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getPercent()
	{
		return percent;
	}

	/**
	 * Returns ~100K/s.
	 *
	 * @return 100000;
	 */
	@Override
	public long getSpeed()
	{
		return 100000;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public long getTransferred()
	{
		return transferred;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isCanceled()
	{
		return cancel;
	}

	/**
	 * Returns false.
	 *
	 * @return false.
	 */
	@Override
	public boolean isTransferred()
	{
		return false;
	}

	/**
	 * Registers the listener, and starts the thread which simulates the file transfer.
	 *
	 * @param listener The listener to register.
	 */
	@Override
	public void registerListener( final FileTransferListener listener )
	{
		this.listener = listener;
		new MockTransferThread().start();
	}

	/**
	 * This thread simulates the transfer process.
	 *
	 * @author Christian Ihle
	 */
	private class MockTransferThread extends Thread
	{
		/**
		 * {@inheritDoc}
		 */
		@Override
		public void run()
		{
			try
			{
				sleep( 500 );
				listener.statusWaiting();
				sleep( 500 );
				listener.statusConnecting();
				sleep( 1000 );
				listener.statusTransferring();
				sleep( 500 );

				while ( !cancel && transferred < getFileSize() )
				{
					transferred += 10000;
					percent += 2;
					sleep( 50 );
					listener.transferUpdate();
				}

				if ( cancel )
					listener.statusFailed();
				else
					listener.statusCompleted();
			}

			catch ( final InterruptedException e )
			{
				e.printStackTrace();
			}
		}
	}
}
